# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Nathan Angelacos <nangel@alpinelinux.org>
# Contributor: TBK <alpine@jjtc.eu>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Duncan Bellamy <dunk@denkimushi.com>
# Maintainer: Duncan Bellamy <dunk@denkimushi.com>
pkgname=rspamd
pkgver=3.10.2
pkgrel=0
pkgdesc="Fast, free and open-source spam filtering system"
url="https://rspamd.com/"
arch="all"
license="Apache-2.0 BSD-1-Clause BSD-2-Clause BSD-3-Clause CC0-1.0 LGPL-2.1-or-later
	LGPL-3.0-only MIT Zlib"
pkgusers="rspamd"
pkggroups="rspamd"
depends="icu-data-full"
makedepends="
	cmake
	curl-dev
	elfutils-dev
	glib-dev
	hiredis-dev
	icu-dev
	libarchive-dev
	libsodium-dev
	libstemmer-dev
	libunwind-dev
	fasttext-dev
	openssl-dev>3
	pcre2-dev
	perl
	ragel
	samurai
	sqlite-dev
	xxhash-dev
	zlib-dev
	zstd-dev
	"
case "$CARCH" in
	ppc64le|riscv64|s390x|x86|loongarch64|a*) makedepends="$makedepends lua5.4-dev"
		_luajit="OFF" ;;
	*) makedepends="$makedepends luajit-dev"
		_luajit="ON" ;;
esac
install="$pkgname.pre-install $pkgname.post-upgrade"
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-client
	$pkgname-libs
	$pkgname-utils::noarch
	$pkgname-openrc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/rspamd/rspamd/archive/$pkgver.tar.gz
	$pkgname.logrotated
	$pkgname.initd
	$pkgname.confd
	10-control_socket.patch
	20-default-configs.patch
	30-unbundle-snowball.patch
	31-unbundle-hiredis.patch
	no-avx-test.patch
	tests.patch
	"
# merged into main package
provides="
	rspamd-controller=$pkgver-r$pkgrel
	rspamd-fuzzy=$pkgver-r$pkgrel
	rspamd-proxy=$pkgver-r$pkgrel
	"
replaces="
	rspamd-controller
	rspamd-fuzzy
	rspamd-proxy
	"

case "$CARCH" in
	x86_64|aarch64|ppc64le)
		makedepends="$makedepends vectorscan-dev"
		_hm="ON" ;;
	*)
		_hm="OFF" ;;
esac

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	case "$CARCH" in
		arm*) local _backward=OFF ;;
		*) local _backward=ON ;;
	esac
	cmake -B build \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCONFDIR=/etc/$pkgname \
		-DRUNDIR=/run/$pkgname \
		-DRSPAMD_USER=$pkgusers \
		-DRSPAMD_GROUP=$pkggroups \
		-DENABLE_FASTTEXT=ON \
		-DSYSTEM_FMT=OFF \
		-DENABLE_BACKWARD="$_backward" \
		-DENABLE_HYPERSCAN="$_hm" \
		-DENABLE_LUAJIT="$_luajit" \
		-DENABLE_PCRE2=ON \
		-DENABLE_URL_INCLUDE=ON \
		-DSYSTEM_XXHASH=ON \
		-DSYSTEM_ZSTD=ON \
		-GNinja \
		$CMAKE_CROSSOPTS
	cmake --build build --target all check
}

check() {
	cmake --build build --target run-test
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	cd "$pkgdir"

	install -D usr/share/"$pkgname"/www/README.md \
		usr/share/doc/"$pkgname"/README.md
	rm usr/share/"$pkgname"/www/README.md

	install -dm 750 -o rspamd -g rspamd \
		var/lib/$pkgname
	install -dm 750 -o rspamd -g rspamd \
		var/log/$pkgname
	install -dm 755 -o rspamd -g rspamd \
		etc/$pkgname/local.d/maps.d

	install -Dm 644 "$srcdir"/$pkgname.logrotated etc/logrotate.d/$pkgname
	install -Dm 755 "$srcdir"/$pkgname.initd etc/init.d/$pkgname
	install -Dm 644 "$srcdir"/$pkgname.confd etc/conf.d/$pkgname
	install -d etc/"$pkgname"/local.d etc/"$pkgname"/override.d
}

client() {
	pkgdesc="$pkgdesc (console client)"

	amove usr/bin/rspamc*
}

libs() {
	pkgdesc="$pkgdesc (libraries)"

	amove usr/lib/$pkgname/*.so
}

utils() {
	pkgdesc="$pkgdesc (utilities)"
	depends="perl"

	amove usr/bin/"$pkgname"_stats
}

sha512sums="
5c71b333705dfe71a47e4cbf880b1ca417c46e254343ede9652fdcb5a9151974fafe88b1475073d43f947a1fe075ac81a70f1ac50693653ba139b65e384edb83  rspamd-3.10.2.tar.gz
2efe28575c40d1fba84b189bb872860e744400db80dce2f6330be6c6287fb3f46e6511284729b957488bf40bcb9b0952e26df9934f5f138334bd2766075c45cb  rspamd.logrotated
c136126e3d3953079939b2b0b9b49f5f921af4c3aa0ad59a206632436ff963a69b5980a3b8e4eb14882d8db31d6fd96045a9e1cd6302ad111d76b8e92fab890e  rspamd.initd
a2003ef0c9d64a44480f59302864a2dfedcbe3a0047fcbb655408bc8aae9014b6ad0ddc6b64d4abeeb21bea0f86678afd30589ac8eed83e07ad7f87710e93702  rspamd.confd
d82ee4c81d5e6401327a6f16cb2cbaa4e968532270ef5ef960319c5aac72720c0ac3a3d42fe8e1f76e20488eadd0d01a5cb313e0fe12979aef1363325dd7c716  10-control_socket.patch
8801e3af59886847c25c87ca2f231cd9ededf35f376f9920967e72696fb017b1a4312f3a49017b52f65556bfe5c9f74a80405643afa32bb2906c38d39c5e1818  20-default-configs.patch
3bb8dd5525e9370e134922c0f37d08b9287432f2f146476ab2dd635e1ea6ccc77143b5b3f721b959dc5721ef2c00f66656d7c5eab4874f81022d803a50b7620e  30-unbundle-snowball.patch
0ea1b61b67e5ee3f2b73ee88c69d7d23d2d4d939a8db45b5189347d4b927d8c34faf5c0da9571e1609312ff4700ae874399f96ebc74569c5f93dbdfd9a7aea98  31-unbundle-hiredis.patch
967f28425105b697f3cdaf38478cd4330ecab814f0296fe6167977156df914a399a685bd72886eaeb262dabab9ccfd061198fffcdfe959dbb988e95a397cf86d  no-avx-test.patch
9c39f295e86aa6e22a95aeca267943d5e037adaa9b9885db27a7c5ef292a15f84de93e7a31d91ad24b83b1d1fe0ca9f1d34478f57e92611520627ed1421810c7  tests.patch
"
